package com.nxy.cpfsfe.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class Encry {
	
	public static String encryString(String value) throws Exception {
        return encryString("Y", "ADTECES", value);
    }

    public static String decryptString(String value) throws Exception {
        return encryString("N", "ADTECES", value);
    }

    private static String encryString(String type, String key, String value) throws Exception {
        if (value != null && !value.equals("")) {
            try {
                byte[] values;
                if (type.equals("Y")) {
                    values = value.getBytes();
                    int temp = values.length % 8;
                    if (temp != 0) {
                        int num = values.length - temp + 8;
                        values = getBytes(value, num);
                    }

                    byte[] keys = getBytes(key, 24);
                    byte[] newValue = encrypt3DES(values, keys);
                    value = new String(Base64.getEncoder().encode(newValue));
                } else {
                    values = getBytes(key, 24);
                    byte[] newValue = decrypt3DES(Base64.getDecoder().decode(value.getBytes()), values);
                    byte b = 0;
                    newValue = removeByte(newValue, b);
                    value = new String(newValue);
                }

                return value;
            } catch (Exception var7) {
                if (type.equals("Y")) {
                    throw new Exception("加密失败", var7);
                } else {
                    throw new Exception("解密失败", var7);
                }
            }
        } else {
            return value;
        }
    }

    public static byte[] decrypt3DES(byte[] src, byte[] key) throws Exception {
        byte[] nullIv = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
        SecretKey securekey = new SecretKeySpec(key, "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        IvParameterSpec iv = new IvParameterSpec(nullIv);
        cipher.init(2, securekey, iv);
        return cipher.doFinal(src);
    }

    public static byte[] encrypt3DES(byte[] src, byte[] key) throws Exception {
        byte[] nullIv = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
        SecretKey securekey = new SecretKeySpec(key, "DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        IvParameterSpec iv = new IvParameterSpec(nullIv);
        cipher.init(1, securekey, iv);
        return cipher.doFinal(src);
    }


    public static byte[] getBytes(String str, int num) {
        byte[] bytes = new byte[num];
        byte[] value = str.getBytes();

        for(int i = 0; i < num; ++i) {
            if (i < value.length) {
                bytes[i] = value[i];
            } else {
                bytes[i] = 0;
            }
        }

        return bytes;
    }

    public static byte[] removeByte(byte[] b, byte c) {
        int i;
        for(i = b.length; i > 0 && b[i - 1] == c; --i) {
        }

        byte[] bytes = new byte[i];

        for(int j = 0; j < i; ++j) {
            bytes[j] = b[j];
        }

        return bytes;
    }

    public static void main(String[] args) {
	    try{
            System.out.println(Encry.decryptString("1bbUC1NQafk="));

        }catch (Exception e){

        }
    }
}
